14  Operators in Python

Operators are the building blocks of any programming language, and understanding how to use them effectively is crucial for writing efficient and readable code in Python.

Operators in Python are special symbols that perform arithmetic or logical computation. The value that the operator operates on is called the operand. Python operators can be classified into several categories:

14.1 Assignment Operators

Assignment operators are used to assign values to variables:

  • = Assigns a value to a variable. E.g., x = 5
  • += Adds right operand to the left operand and assign the result to left operand. E.g., x += 5
  • -= Subtracts right operand from the left operand and assign the result to left operand. E.g., x -= 5
  • *= and others similarly modify and assign.
Code
Assignment operators
# Assign
x = 5
x
5
Code
# Add and assign
x += 3
x
8
Code
# Subtract and assign
x -= 2
x 
6
Code
# Multiplication and assign
x *= 2
x
12

14.2 Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.

  • + Addition: Adds two operands. E.g., x + y
  • - Subtraction: Subtracts the right operand from the left operand. E.g., x - y
  • * Multiplication: Multiplies two operands. E.g., x * y
  • / Division: Divides the left operand by the right operand. E.g., x / y
  • % Modulus: Returns the remainder when the left operand is divided by the right operand. E.g., x % y
  • ** Exponent: Left operand raised to the power of right operand. E.g., x ** y
  • // Floor Division: Division that results into whole number adjusted to the left in the number line. E.g., x // y
Code
Python Arithmetic operators
# Addition
5 + 3  
8
Code
# Subtraction
10 - 3 
7
Code
# Multiplication
4 * 3
12
Code
# Division
8 / 2
4.0
Code
# Modulus
9 % 4
1
Code
# Exponent
2 ** 3
8
Code
# Floor Division
17 // 4
4

14.3 Comparison Operators

Comparison operators are used to compare values. It either returns True or False according to the condition.

  • == Equal: True if both operands are equal. E.g., x == y
  • != Not equal: True if operands are not equal. E.g., x != y
  • > Greater than: True if left operand is greater than the right operand. E.g., x > y
  • < Less than: True if left operand is less than the right operand. E.g., x < y
  • >= Greater than or equal to: True if left operand is greater than or equal to the right operand. E.g., x >= y
  • <= Less than or equal to: True if left operand is less than or equal to the right operand. E.g., x <= y
Code
Python Comparison Operators
# Equal
5 == 3
False
Code
# Not equal
5 != 3
True
Code
# Greater than
5 > 3
True
Code
# Less than
5 < 3
False
Code
# Greater than or equal to
5 >= 5
True
Code
# Less than or equal to
5 <= 3
False

14.4 Logical Operators

Logical operators are used to combine conditional statements:

  • and Returns True if both statements are true. E.g., x < 5 and x < 10
  • or Returns True if one of the statements is true. E.g., x < 5 or x < 4
  • not Reverse the result, returns False if the result is true. E.g., not(x < 5 and x < 10)
Code
Python Logical operators
# and
(5 > 3) and (5 > 4)
True
Code
# or
(5 > 3) or (5 < 4)
True
Code
# not
not(5 > 3)
False